package com.bootssm.controller;

import com.bootssm.entity.*;
import com.bootssm.service.AddresslistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/addresslist")
public class AddresslistController {
    @Autowired
    AddresslistService addresslistService;

    @PostMapping("/findAll")
    public PageView<Contact> findAll(@RequestBody ContactVO contactVO){
        Integer page = contactVO.getPage();
        Integer start = (page-1)*contactVO.getSize();
        contactVO.setStart(start);
        List<Contact> contactList= addresslistService.findAll(contactVO);
        Integer count = addresslistService.getCount(contactVO);
        PageView<Contact> pageView = new PageView<>();
        pageView.setDataList(contactList);
        pageView.setCount(count);
        return pageView;
    }

    @PostMapping("/insert")
    public Result insert(@RequestBody Contact contact) {
        try {
            addresslistService.insert(contact);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("500", "save wrong!");
        }
        return new Result("200", "save!");
    }

    @PostMapping("/update")
    public Result update(@RequestBody Contact contact){
        try {
            addresslistService.update(contact);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("500", "update wrong!");
        }
        return new Result("200", "update!");
    }

    @GetMapping("/delete/{id}")
    public Result deleteById(@PathVariable("id") String contactid){
        try {
            addresslistService.deleteById(contactid);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("500", "delete wrong!");
        }
        return new Result("200", "delete!");
    }

    @GetMapping("/findById/{id}")
    public Contact findById(@PathVariable("id") String contactid) {
        return addresslistService.findById(contactid);
    }
}
